<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>新增角色</title>
    <link rel="stylesheet" href="layui/css/layui.css">
    <link rel="stylesheet" href="css/css.css"/>
</head>
<body>
<div class="adduser">
    <form class="login layui-form" style="margin-top: 10px">
        <div class="user-name layui-form-item">
            <i class="layui-icon layui-icon-username"></i>
            <input name="name" placeholder='角色名称' maxlength="16" required lay-verify='required|rolename'
                   class="layui-input"/>
        </div>
        <div class="password layui-form-item">
            <i class="layui-icon layui-icon-note"></i>
            <input name="note" placeholder='备注' class="layui-input"/>
        </div>
        <div class="layui-form-item">
            <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="save">保存</button>
        </div>
    </form>
</div>
</body>
<script src="layui/layui.js"></script>
<script src="js/common.js" charset="utf-8"></script>
<script>
    layui.use(['table', 'layer', 'jquery', 'form'], function () {
        var    layer = layui.layer,
                form = layui.form,
                $ = layui.jquery;

        form.verify({
            rolename:function(value, item) {//value：表单的值、item：表单的DOM对象
                if (!new RegExp("^[a-zA-Z]+$").test(value)) {

                    return '角色名只能使用英文';

                }
                if (!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
                    return '角色名不能有特殊字符';
                }
                if (/(^\_)|(\__)|(\_+$)/.test(value)) {
                    return '角色名首尾不能出现下划线\'_\'';
                }
                if (/^\d+\d+\d$/.test(value)) {
                    return '角色名不能全为数字';
                }

                //验证用户名是否存在
                var exist;

                //发送登录名校验请求
                doAjax($,layer,'POST',false,{"name":value},'role/addCheckRoleName',afterlLoginName);

                //原始密码校验之后的回调函数
                function afterlLoginName(result) {
                    if (result.status == '200') {
                        exist = true;
                    }else{
                        exist = false;
                    }
                }

                if (exist){
                    return "角色名已存在!";
                }
            }
        });

        form.on('submit(save)', function (data) {

            var field = data.field;

            field.createUser=layui.data('userData').data.loginName;

            //发送添加请求
            doAjax($, layer, 'POST', false, field, '/role/add', afterAdd);

            //保存成功回调函数
            function afterAdd(result) {
                if (result.status == "200") {
                    //保存成功的提示与跳转
                    layer.msg('保存成功', {
                        icon: 1
                        , time: 1500
                    }, function () {
                        //iframe页面关闭自身
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                        //刷新父级数据列表
                        window.parent.location.reload();
                    });

                } else {
                    layer.msg(result.msg);
                }

            }

            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });


    })
</script>
</html>
